package com.xdl.flytest.util.runner;

import com.xdl.flytest.util.docker.DockerEnvironmentConfig;
import com.xdl.flytest.util.docker.DockerManager;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;

/**
 * @Auther: 80736
 * @Date: 2019/1/31 21:50
 * @Description:
 */

@Component
public class BaseRunner implements Runner {

    @Autowired
    private TestPreProcessor preProcessor;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void run(String containerId, String script) throws IOException, InterruptedException {

    }

    @Override
    public void run(String containerId) {

    }

    @Override
    public void stop(Integer id) {

    }

    @Override
    public String init() {
        return preProcessor.process();
    }

    @Override
    public boolean cleanUp() {
        try {
            // 清空本地json文件夹（防止上次执行的数据干扰）
            FileUtils.deleteDirectory(new File(DockerEnvironmentConfig.JSON_FILE_PATH));
            logger.debug("clean local json dir");
            // 删除容器
            return DockerManager.removeContainer(DockerEnvironmentConfig.CONTAINER_NAME);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }
}
